package com.yz.jdd.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.yz.jdd.R;
import com.yz.jdd.base.MyApplication;
import com.yz.jdd.module.main.MainActivity;
import com.yz.jdd.module.main.entity.MenuInfoBean;
import com.yz.jdd.webview.Router;

import java.util.ArrayList;
import java.util.List;

public class AdDialog extends Dialog {
    List<MenuInfoBean.ConfigBean> alertAdList;
    /**
     * 广告banner
     */
    private Banner banner_blue_sea;
    ImageView close_addialog;
    public AdDialog(@NonNull Context context) {
        super(context, R.style.Dialog);
    }

    public AdDialog(Context context, List<MenuInfoBean.ConfigBean> alertAdList) {
        super(context,  R.style.Dialog);
        this.alertAdList=alertAdList;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_adlist);
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.gravity = Gravity.CENTER;
        p.height = WindowManager.LayoutParams.MATCH_PARENT;
        p.width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(p);

        initView();
        initBanner();
    }
    private void initView(){
        banner_blue_sea = (Banner)findViewById(R.id.banner_blue_sea);
        close_addialog=findViewById(R.id.close_addialog);
        close_addialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }
    private void initBanner(){
        ArrayList images = new ArrayList<>();
        for (MenuInfoBean.ConfigBean configBean : alertAdList) {
            images.add(configBean.imgUrl);
        }
        banner_blue_sea.setImages(images);
        banner_blue_sea.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner_blue_sea.setImageLoader(new GlideImageLoader());
        banner_blue_sea.isAutoPlay(true);
        banner_blue_sea.setDelayTime(2000);
        banner_blue_sea.setIndicatorGravity(BannerConfig.CENTER);
        banner_blue_sea.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                MenuInfoBean.ConfigBean configBean = alertAdList.get(position);
                if ("ali".equals(configBean.router)) {
                    //跳转支付宝
                    try {
                        String url = "alipays://platformapi/startapp?appId=20000067&url=https%3A%2F%2Frender.alipay.com%2Fp%2Ff%2Ffd-jsb0un9e%2Fpages%2Fregister%2Findex.html%3FactivityCode%3DLANHAI_JL_20190122%26__webview_options__%3DtransparentTitle%253Dalways";
                        MyApplication.application.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(url)));
                    } catch (Exception e) {
                        Toast.makeText(MyApplication.application, "请安装最新版本支付宝", Toast.LENGTH_SHORT).show();
                    }
                } else {

                    Router.startNewWebView(MyApplication.application, configBean.router,configBean.backUrl, configBean.title, "server".equals(configBean.target));
                }
            }
        });
        banner_blue_sea.start();
    }
}
